package com.tim.hospital;

import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.stream.Collectors;

public class ErTongHospital {
    String baseDir="D:\\个人资料\\桐桐住院\\20250707\\";
    String sourceInfoFile=baseDir+"t";
    String targetFile=baseDir+"儿童医院一日清单";
    int autoAppend=1;

    /**住院一日清单*/
    @Test
    public void feeParseMulti() throws IOException {
        autoAppend=0;
        String splitor = "李雨桐 ";
        String str = Files.readAllLines(Paths.get(sourceInfoFile))
                .stream().collect(Collectors.joining("\n"));
        String[] split = str.split(splitor);
        for (int i = 1; i < split.length; i++) {
            String[] strs = (splitor+split[i]).trim().split("\n");
            if(strs.length<2){
                continue;
            }
            doFeeParse(Arrays.asList(strs));
        }
    }

    /**住院一日清单*/
    @Test
    public void feeParse() throws IOException {
        List<String> list = Files.readAllLines(Paths.get(sourceInfoFile));
        doFeeParse(list);
    }

    private void doFeeParse(List<String> list ) throws IOException{

        StringBuilder builder = new StringBuilder();
        String date="";
        int num=0;
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if(i==0){
                date=s.split("\\s+")[1];
//				builder.append("时间").append("\t");
                continue;
            }else if(i<6){
//				builder.append(s).append("\t");
//				if(i==5){
//					builder.append("\n");
//				}
                continue;
            }
            if(num==0){
                builder.append(date).append("\t");
                builder.append("").append("\t");
            }
            builder.append(s).append("\t");
            num++;
            if(num==4){
                builder.append("\n");
                num=0;
            }
        }
        String string = builder.toString().trim();
        System.out.println(string);
        if(autoAppend==1){
            Files.write(Paths.get(targetFile), Arrays.asList(string), StandardOpenOption.APPEND);
        }
    }

    /**信息汇总*/
    @Test
    public void feeAnalysis() throws IOException {
        List<String> list = Files.readAllLines(Paths.get(targetFile));
        Map<String, BigDecimal> map = new HashMap<>();

        list.stream()
                .skip(1)
                .forEach(str->{
                    String[] split = str.split("\t");
                    BigDecimal value = new BigDecimal(split[3]);
                    map.put(split[0], map.getOrDefault(split[0], BigDecimal.ZERO).add(value));
                    map.put(split[0]+split[4], map.getOrDefault(split[0]+split[4], BigDecimal.ZERO).add(value));
                    map.put(split[4], map.getOrDefault(split[4], BigDecimal.ZERO).add(value));
                });
        map.keySet()
                .stream()
                .sorted(Comparator.<String>naturalOrder().reversed())
                .forEach(k->System.out.println(k+"\t"+map.get(k)));

    }
}
